package thirt.view
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.net.URLRequest;
	
	import thirt.component.TextSprite;
	
	public class InfoToView
	{
		private var _data:Object;
		public var key:String;
		public function InfoToView()
		{
		}
		public function set data(value:Object):void{
			this._data = value;
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onCom);
			if(_data.type == "movie"){
				loader.load(new URLRequest(_data.url));
			}else if(_data.type == "img"){
				loader.load(new URLRequest(_data.url));
			}else if(_data.type == "text"){
				setTxt();
			}
		}
		private function onCom(event:Event):void{
			var console:Console;
			if(_data.type == "movie"){
				var cls:Class = Class(event.target.applicationDomain.getDefinition("icon"));
				var movie:MovieClip = new cls();
				var obj:Object = new Object;
				obj.color = _data.color;
				obj.url = _data.url;
				obj.id = _data.id;
				
				/* movie.graphics.beginFill(0xff0000,0);
				movie.graphics.drawRect(0,0,movie.width,movie.height);
				movie.graphics.endFill(); */
				
				var sp:Sprite = new Sprite();
				sp.graphics.beginFill(0xff0000,0);
				sp.graphics.drawRect(0,0,movie.width,movie.height);
				sp.graphics.endFill();
				movie.addChild(sp);
				
				console = EditSpace.getInstance().addswf(obj,movie,false,key);
				console.x = _data.x;
				console.y = _data.y;
				console.setContentRotation(_data.rotation);
				console.runNewPoint(_data.scale);
				console.drawLine();
				console.select = false;
				if(_data.edit == 1)
					setTagColor(movie,_data.color);
				
			}else if(_data.type == "img"){
				var bitmap:Bitmap = event.target.content;
				bitmap.smoothing = true;
				
				var sc:Number = _data.baseScale;
				var ma:Matrix = new Matrix;
				ma.scale(sc,sc);
				
				var bitmapdata:BitmapData = new BitmapData(bitmap.bitmapData.width*sc,bitmap.bitmapData.height*sc,true,0);
				bitmapdata.draw(bitmap,ma);
				var objs:Object = new Object;
				objs.url = _data.url;
				objs.baseScale = sc;
				
				console = EditSpace.getInstance().addImg(objs,new Bitmap(bitmapdata),false,key);
				console.x = _data.x;
				console.y = _data.y;
				console.setContentRotation(_data.rotation);
				console.runNewPoint(_data.scale);
				console.drawLine();
				console.select = false;
			}
		}
		
		private function setTagColor(movie:MovieClip,colorStr:String):void{
			var colorAry:Array = colorStr.split(",");
			for(var i:int=0;i<colorAry.length;i++){
				var str:String = "0x" + colorAry[i];
				colorAry[i] = parseInt(str, 16);
			}
			for(i=0;i<colorAry.length;i++){
				var color:uint = colorAry[i];
				var blue:int = color & 0xFF;
				var green:int = (color >> 8) & 0xFF;
				var red:int = (color >> 16) & 0xFF;
				var colorT:ColorTransform = new ColorTransform(0,0,0,1,red,green,blue,0);
				if(i == 0){
					if(movie.first){
						MovieClip(movie.first).transform.colorTransform = colorT;
					}else{
						MovieClip(movie).transform.colorTransform = colorT;
					}
				}else if(i == 1){
					if(movie.second){
						MovieClip(movie.second).transform.colorTransform = colorT;
					}
				}else if(i == 2){
					if(movie.third){
						MovieClip(movie.third).transform.colorTransform = colorT;
					}
				}
			}
			
		}
		
		private function setTxt():void{
			var console:Console = EditSpace.getInstance().addText(false,key);
			console.runNewPoint(_data.scale);
			console.x = _data.x;
			console.y = _data.y;
			var text:TextSprite = console.targets as TextSprite;
			text.text = _data.info.text;
			text.align = _data.info.align;
			text.color = _data.info.color;
			text.size = _data.info.size;
			text.fontface = _data.info.font;
			text.B = _data.info.bold;
			text.I = _data.info.italic;
			text.textWidth = _data.info.textWidth;
		}

	}
}